package com.marc.eden;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;

public class HttpCommunication {
    private URL url;
    private HttpURLConnection httpURLConnection;
    private static String sessionID;
    private String message;

    public HttpCommunication() {
        setUrl(null);
        setMessage(null);
    }

    public HttpCommunication(URL url, String message) {
        setUrl(url);
        setMessage(message);
        if (url != null) {
            try {
                System.out.println("sessionID: "+sessionID);
                httpURLConnection = (HttpURLConnection) url.openConnection();
                if (sessionID != null) {
                    httpURLConnection.setRequestProperty("cookie", sessionID);
                }
                httpURLConnection.setRequestMethod("POST");
                httpURLConnection.setDoOutput(true);
                httpURLConnection.setDoInput(true);
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else {
            System.out.println("No URL !");
        }
    }

    public void setUrl(URL url) {
        this.url = url;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public void sendMessage() {
        if (url == null) {
            System.out.println("No URL !");
            return;
        } else if (message == null) {
            System.out.println("No message !");
            return;
        } else if (httpURLConnection == null) {
            try {
                httpURLConnection = (HttpURLConnection) url.openConnection();
                if (sessionID != null) {
                    httpURLConnection.setRequestProperty("cookie", sessionID);
                }
                httpURLConnection.setRequestMethod("POST");
                httpURLConnection.setDoOutput(true);
                httpURLConnection.setDoInput(true);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        try {
            PrintWriter printWriter = new PrintWriter(httpURLConnection.getOutputStream());
            printWriter.write(message);
            printWriter.flush();
            printWriter.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public String getResponse() {
        if (url == null) {
            System.out.println("No URL !");
            return null;
        } else if (httpURLConnection == null) {
            try {
                httpURLConnection = (HttpURLConnection) url.openConnection();
                if (sessionID != null) {
                    httpURLConnection.setRequestProperty("cookie", sessionID);
                }
                httpURLConnection.setRequestMethod("POST");
                httpURLConnection.setDoOutput(true);
                httpURLConnection.setDoInput(true);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        try {
            BufferedInputStream bis = new BufferedInputStream(httpURLConnection.getInputStream());
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
//            if (sessionID == null) {
//                sessionID = httpURLConnection.getHeaderField("Set-Cookie");
//                sessionID = sessionID.substring(0, sessionID.indexOf(";"));
//                System.out.println("sessionID: "+sessionID);
//            }
            int len;
            byte[] arr = new byte[1024];
            while((len=bis.read(arr))!= -1){
                bos.write(arr,0,len);
                bos.flush();
            }
            bos.close();
            return bos.toString("utf-8");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
